﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
    public class ButtonText : Button
    {
        public ButtonText(Vector2 position, string text, SpriteFont font, Color color, Color color2) : base(position, null)
        {
            this.Text = text;
            this.Font = font;
            this.width = (int)font.MeasureString(text).X;
            this.height = (int)font.MeasureString(text).Y;
            this.ClickBox = new Rectangle((int)position.X - (width / 2), (int)position.Y - (height / 2), width, height);
            this.TextColor = color;
            this.TextColorTouched = color2;
            this.canplaytouchesound = true;
        }

        // Click box du boutton
        private Rectangle ClickBox;

        // Texte du boutton
        public string Text;
        private SpriteFont Font;
        private int width;
        private int height;
        public Color TextColor;
        public Color TextColorTouched;
        private bool Touched;

        // Peut-on jouer le son de clickage de boutton.
        private bool canplaytouchesound = true;

        public override bool IsPressed(MouseState mouse)
        {
            bool result = mouse.LeftButton == ButtonState.Pressed
                && this.IsTouched(mouse);
            if (result)
            {
                GameSound.ButtonClick.Play();
                Game.CanClickLeft = false;
            }

            return result;
        }
        

        public virtual bool IsPressedRight(MouseState mouse)
        {
            bool result = mouse.RightButton == ButtonState.Pressed
                && this.IsTouched(mouse);
            if (result)
            {
                GameSound.ButtonClick.Play();
                Game.CanClickRight = false;
            }

            return result;
        }

        public override bool IsTouched(MouseState mouse)
        {
            bool result =  ClickBox.Contains(mouse.X, mouse.Y)
                && Game.CanClickLeft && Game.CanClickRight;

            if (result && canplaytouchesound)
            {
                Game.CanClickLeft = true;           
                GameSound.ButtonTouche.Play();
                canplaytouchesound = false;
                Touched = true;
            } 

            // Ne jouer le son de toucher le boutton sans que ça spamme
            if (!result)
            {
                canplaytouchesound = true;
                Touched = false;
            }

            return result;
        }



        public void Update(MouseState mouse)
        {
            IsTouched(mouse);
        }

        public void ChangeText(string newtext)
        {
            this.Text = newtext;
            this.width = (int)Font.MeasureString(newtext).X;
            this.height = (int)Font.MeasureString(newtext).Y;
            this.ClickBox = new Rectangle((int)Position.X - (width / 2), (int)Position.Y - (height / 2), width, height);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(Font, Text, new Vector2(Position.X - (width / 2), Position.Y - (height / 2)), Touched ? TextColorTouched : TextColor);
        }
    }
}
